package com.xiaoxu.rental.controller;

import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaoxu.rental.entity.AutoBrand;
import com.xiaoxu.rental.service.AutoBrandService;
import com.xiaoxu.rental.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

/**

 * @author xiaoxu
 * @since 2024-07-24
 */
@Api(tags = "汽车品牌管理")
@RestController
@RequestMapping("/rental/autoBrand")
@SuppressWarnings("all")
public class AutoBrandController {
    @Resource
    private AutoBrandService autoBrandService;

    @ApiOperation(value = "分页查询汽车品牌")
    @PostMapping("/{start}/{size}")
    public Result search(@PathVariable int start,
                         @PathVariable int size,
                         @RequestBody AutoBrand autoBrand) {
        Page<AutoBrand> page = new Page<>(start, size);
        return Result.success()
                .setData(autoBrandService.searchByPage(page, autoBrand));
    }
    @ApiOperation(value = "新增汽车品牌")
    @PostMapping("/save")
    public Result save(@RequestBody AutoBrand autoBrand) {
        return autoBrandService.save(autoBrand) ?
                Result.success() : Result.fail();
    }

    @ApiOperation(value = "修改汽车品牌")
    @PutMapping("/update")
    public Result update(@RequestBody AutoBrand autoBrand) {
        autoBrand.setUpdateTime(LocalDateTime.now());
        return autoBrandService.updateById(autoBrand) ?
                Result.success() : Result.fail();
    }


    @ApiOperation(value = "删除汽车品牌")
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable String ids) {
        List<Integer> list = Arrays.stream(ids.split(","))
                .map(Integer::parseInt)
                .toList();
        return autoBrandService.removeByIds(list) ?
                Result.success() : Result.fail();
    }

    @ApiOperation(value = "根据制造商ID查询汽车品牌")
    @GetMapping("/selectByMakerId/{makerId}")
        public Result selectByMakerId(@PathVariable Integer makerId) {
        return Result.success()
                .setData(autoBrandService.selectByMakerId(makerId));
}

    @GetMapping("/hasChildren/{id}")
    public Result hasChildren(@PathVariable Integer id){
        return autoBrandService.hasChildren(id)?
                Result.success().setMessage("有"):
                Result.success().setMessage("无");
    }
    @GetMapping("exportExcel")
    public void export(HttpServletResponse response) throws IOException {
        List<AutoBrand> list=autoBrandService.list();
        ExcelWriter writer= ExcelUtil.getWriter(true);
        writer.addHeaderAlias("brandName","品牌名称");
        writer.addHeaderAlias("deleted","是否删除");
        writer.write(list,true);
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset:utf-8");
        String fileName= URLEncoder.encode("汽车品牌", StandardCharsets.UTF_8);
        response.setHeader("ContentDisposition","attachment;filename="+fileName+".xlsx");
        writer.flush(response.getOutputStream(),true);
        writer.close();
    }
}
